﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Evan.BLL
{
    /// <summary>
    /// 一些逻辑在这里实现
    /// </summary>
    public class Base
    {

      

        #region 定时发布
        /// <summary>
        /// 获取当前表是否支持定时发布 , 支持的话返回true
        /// </summary>
        /// <param name="curTableName">表名字</param>
        /// <returns></returns>
        public bool GetHasReleaseTime(string curTableName)
        {
            bool has = Config.BaseConfig.ReleaseTimeTable.Contains(curTableName.ToLower());
            return has;
        }
        /// <summary>
        /// 根据传入的栏目动态类 , 获取栏目是否可以支持定时发布
        /// </summary>
        /// <param name="curChannel"></param>
        /// <returns></returns>
        public bool GetHasReleaseTime(dynamic curChannel)
        {
            string curTableName = GetTableByPageType(curChannel);
            return GetHasReleaseTime(curTableName);
        }
        /// <summary>
        /// 根据栏目获取对应的表
        /// </summary>
        /// <param name="curChannel">传入栏目的动态类型</param>
        /// <returns></returns>
        public string GetTableByPageType(dynamic curChannel)
        {
            return Template.TemplateHelper.GetTableNameByPageType(curChannel.PageType);
        }

        /// <summary>
        /// 获取传入栏目是否有定时发布 , 
        /// <para>其中包含了  栏目对应表是否有以及栏目是否开启定时发布</para>
        /// </summary>
        /// <param name="curChannel"></param>
        /// <returns></returns>
        public bool GetCurChannelHasRelease(dynamic curChannel)
        {
            //先看单边表是否支持定时发布
            bool has = GetHasReleaseTime(curChannel);
            if (has)
            {
                //如果当前表支持定时发布 , 再看下当前栏目是否支持定时发布
                has = curChannel.IsReleaseTime == 1; //如果支持 是1   不支持是0
            }
            return has;
        }
        #endregion

        
        #region 根据具体内容ID获取对应的URL
        /// <summary>
        /// 根据当前model获取对应的内容链接
        /// default , list , detail , singlepage
        /// pageNum是获取分页的链接 , linkType 为 list 的时候才可以使用 
        /// 主要用于获取详细页链接
        /// </summary>
        /// <param name="model"></param>
        /// <param name="thisChannel">栏目 , 必填参数</param>
        /// <param name="hasSuffix">是否强制带上 页面名称  默认不用 ( 对 第一页/栏目页/单页生效)</param>
        /// <param name="linkType">default , list , detail , singlepage , 默认是detail</param>
        /// <param name="pageNum">默认是0 如果是其他页的话就传页码进来</param>
        /// <returns></returns>
        public string GetUrlByModel(dynamic model,dynamic thisChannel, bool hasSuffix = false, string linkType = "detail", int pageNum = 0)
        {
            string linkurl = "javascript:;";
            
            //拿到这个栏目的内容
            if (linkType.ToLower() == "singlepage")
            {
                //单页 , 直接返回栏目目录
                linkurl = thisChannel.WebPath;
                if (hasSuffix)
                {
                    linkurl = linkurl + "index.html";
                }
            }
            if (linkType.ToLower() == "default")
            {
                //综合页  ,  直接返回栏目目录
                linkurl = thisChannel.WebPath;
                if (hasSuffix)
                {
                    linkurl = linkurl + "default.html";
                }
            }
            if (linkType.ToLower() == "list")
            {
                //列表页 按照分页查看
                if (pageNum > 0)
                {
                    if (!string.IsNullOrEmpty(thisChannel.RewriteList))
                    {
                        linkurl = thisChannel.WebPath + ((thisChannel.RewriteList).Replace("{Page}", pageNum.ToString()));
                    }
                }
                else
                {
                    linkurl = thisChannel.WebPath;
                    if (hasSuffix)
                    {
                        linkurl = linkurl + "index.html";
                    }
                }
            }
            if (linkType.ToLower() == "detail")
            {
                //如果没有填详细页链接的话 , 返回空链接
                if (!string.IsNullOrEmpty(thisChannel.RewriteDetail))
                {
                    linkurl = thisChannel.WebPath;
                    //详细页
                    //先拿到伪静态配置列表 , 然后遍历列表 , 对比当前拿到的是那个正则名称 , 然后在将model转为dic 在里面拿属性 即可
                    foreach (var item in GetRewriteFieldList())
                    {
                        if ((thisChannel.RewriteDetail).Contains("{" + item + "}"))
                        {
                            //如果存在值
                            Dictionary<string, object> dic = Evan.Model.ConvertModel.ConvertToDictionary(model);
                            linkurl = linkurl + ((thisChannel.RewriteDetail).Replace("{" + item + "}", dic[item].ToString()));
                            break;
                        }
                    }
                }

            }

            return linkurl;
        }
        /// <summary>
        /// 从ext.config中获取伪静态字段 
        /// </summary>
        /// <returns></returns>
        private List<string> GetRewriteFieldList()
        {
            List<string> itemList = new List<string>();
            object itemList_obj = Evan.Common.CacheHelp.GetValue("RewriteFieldList");
            if (itemList_obj == null)
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(Evan.Common.DirFile.ReadFile("~/App_Data/SysConfig/ext.config"));
                XmlNode xn = xmldoc.SelectSingleNode(string.Format("/root/extended[@type='{0}']", "rewrite-field-reg"));
                XmlNodeList nodelist = xn.ChildNodes;  //得到该节点的子节点
                if (nodelist.Count > 0)
                {
                    foreach (XmlElement el in nodelist)//读元素值
                    {
                        //Evan.SystemLog.MemLog.AddExLog2("读取ext.config", null, nodelist.Count.ToString());
                        //读取所有的
                        itemList.Add(el.Attributes["key"].Value);
                        //if (el.Attributes["type"].Value == "Field")
                        //{
                        //    itemList.Add(el.Attributes["key"].Value);
                        //}
                    }
                    if (itemList.Count > 0)
                    {
                        //存缓存
                        Evan.Common.CacheHelp.Insert_NoAbsolute_Rank("RewriteFieldList", itemList, Evan.Common.CacheHelp.CacheTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable);
                    }
                }
            }
            else
            {
                itemList = (List<string>)itemList_obj;
            }

            return itemList;
        }
        #endregion

        



    }
}
